package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
//角色管理
@RestController
@RequestMapping("/role")
public class RoleController {
    @Reference//查找服务
    private RoleService roleService;

    //新增权限管理项
    @RequestMapping("/add")
    public Result add(@RequestBody Role role){
        try{
            roleService.add(role);
        }catch (Exception e){
            e.printStackTrace();
            //服务调用失败
            return new Result(false, "添加权限管理项失败");
        }
        return  new Result(true, "添加权限管理项成功");
    }
    //角色管理项分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = roleService.pageQuery(queryPageBean);
        return pageResult;
    }

    @RequestMapping("/findAll")
    public Result findAll(){
        try{
            List<Role> list = roleService.findAll();
            return  new Result(true, "获取角色信息成功",list);
        }catch (Exception e){
            e.printStackTrace();
            //服务调用失败
            return new Result(false, "获取角色信息失败");
        }
    }
}
